package cn.itcast.item.Service.impl;

import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.dto.item.SearchItemDTO;
import cn.itcast.hmall.pojo.item.Item;
import cn.itcast.item.Service.itemService;
import cn.itcast.item.mapper.ItemMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;


@Service
public class itemImpl extends ServiceImpl<ItemMapper, Item> implements itemService {

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 分页查询商品
     * @param searchItemDTO
     * @return IPage<Item>
     */
    @Override
    public IPage<Item> PaginatedQueries(SearchItemDTO searchItemDTO) {
        //LambdaQueryWrapper查询
        LambdaQueryWrapper<Item> wrapper = Wrappers.lambdaQuery();
        //获取当前页和页数大小
        Page<Item> page =new Page<>(searchItemDTO.getPage(), searchItemDTO.getSize());
        //获取商品名字
        String name = searchItemDTO.getName();
        //非空判断
        if (StringUtils.isNotBlank(name)) {
            wrapper.like(Item::getName,name);
        }

        Date beginDate = searchItemDTO.getBeginDate();
        Date endDate = searchItemDTO.getEndDate();

        if (beginDate != null && endDate != null) {
            String begin = new SimpleDateFormat("yyyy-MM-dd ").format(beginDate);
            String end = new SimpleDateFormat("yyyy-MM-dd ").format(endDate);
            wrapper.between(Item::getCreateTime,begin,end);
        }
        String brand = searchItemDTO.getBrand();
        if (StringUtils.isNotBlank(brand)) {
            wrapper.like(Item::getBrand,brand);
        }

        String category = searchItemDTO.getCategory();
        if (StringUtils.isNotBlank(category)) {
            wrapper.like(Item::getCategory,category);
        }

        itemMapper.selectPage(page, wrapper);

        return page;
    }
    /**
     * 根据id查询
     * @param id
     * @return Item
     */
    @Override
    public Item id(Long id) {

        Item item = itemMapper.selectById(id);
        return item;
    }

    /**
     * 增加商品
     * @param item
     * @return ResultDTO
     */
    @Override
    public ResultDTO add(Item item) {
        //获取时间
        Date date = new Date();
        //设置时间信息字段
        item.setCreateTime(date);
        item.setUpdateTime(date);
        //添加请求参数
        int insert = itemMapper.insert(item);
        //返回结果
        System.err.println(insert==0?ResultDTO.error():ResultDTO.ok());
        return insert==0?ResultDTO.error():ResultDTO.ok();
    }
    /**
     * 商品上架、下架
     * @param id
     * @param status 上下架状态
     * @return
     */
    @Override
    public ResultDTO onDown(Long id, Integer status) {

        LambdaQueryWrapper<Item> wrapper = Wrappers.lambdaQuery();

        Item item = new Item();

        item.setStatus(status);//前端是什么操作就修改为什么状态

        wrapper.eq(Item::getId,id);

        int update = itemMapper.update(item, wrapper);//根据条件更新

        if (update == 1){
            return ResultDTO.ok();
        }else {
           return ResultDTO.error();
        }
    }
    /**
     * 商品修改
     * @param item
     * @return ResultDTO
     */
    @Override
    public ResultDTO revise(Item item) {

        LambdaQueryWrapper<Item> wrapper = Wrappers.lambdaQuery();

        wrapper.eq(Item::getId,item.getId());

        int update = itemMapper.update(item, wrapper);

        return update==0?ResultDTO.error():ResultDTO.ok();
    }
    /**
     * 商品删除
     * @param id
     * @return ResultDTO
     */
    @Override
    public ResultDTO Delete(Long id) {
        Item item = id(id);
        //判断商品在下架状态
        if (item.getStatus() == 1){
            return ResultDTO.error("请先下架");
        }

        int delete = itemMapper.deleteById(id);

        return delete==0?ResultDTO.error():ResultDTO.ok();

    }

    @Override
    public void stock(Long itemId, Integer num) {
        Item item = getById(itemId);
        item.setStock(item.getStock() - num);
        updateById(item);
    }


}



